<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Student;
use App\Models\SystemUser;
use Illuminate\Http\Request;
use mysql_xdevapi\Exception;


class WechatController extends CommonController
{
    //获取微信id
    public function getCompanyOpenid(Request $request){
        if($request->filled('code')){
            $oauth = $this->wechatApp->getOAuth();
            try{
                $wxuser = $oauth->userFromCode($request->code);
                $openid = $wxuser->getId();
                if($openid){
                    $student = Company::query()->where('openid',$openid)->first();
                    if($student){
                        return $this->success($student);
                    }
                    return $this->success($openid);
                }
            }catch (Exception $exception){
                return $this->failed($exception->getMessage(),201);
            }
            return $this->success($wxuser->getId());
        }else{
            return $this->failed('请提交code',201);
        }
    }
    //获取微信id
    public function getOpenid(Request $request){
        if($request->filled('code')){
            $oauth = $this->wechatApp->getOAuth();
            try{
                $wxuser = $oauth->userFromCode($request->code);
                $openid = $wxuser->getId();
                if($openid){
                    $student = Student::query()->where('openid',$openid)->first();
                    if($student){
                        return $this->success($student);
                    }
                    return $this->success($openid);
                }
            }catch (Exception $exception){
                return $this->failed($exception->getMessage(),201);
            }
            return $this->success($wxuser->getId());
        }else{
            return $this->failed('请提交code',201);
        }
    }
    public function webStore(Student $model,Request $request){
        if($request->filled('id')){
            $data = $request->except('id');
            $student = $model->find($request->id);
            $student->update($data);
            return $this->success('提交成功');
        }
        if($request->filled('openid')){
            $student = $model->where('openid',$request->openid)->first();
            if($student){
                return $this->success($student);
            }
            $data = $request->all();
                $data['openid'] = $request->openid;
            }
            if($request->filled('user_id')){
                $user = SystemUser::query()->find($request->user_id);
                $data['operator'] = $user->name;//$user->name;
            }
            $model->fill($data);
            $model->save();
            return $this->success($model);
    }
    public function setJsSdk(){
        $utils = $this->wechatApp->getUtils();
        $config = $utils->buildJsSdkConfig(
            'http://web.zjhzgroup.com/',
            [],
            [],
            true,
        );

        $config['ticket'] =  $this->wechatApp->getTicket();
//        $utils->getTicket();
        return $config;
    }
}
